package io.renren.modules.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.basic.dao.*;
import io.renren.modules.basic.entity.*;
import io.renren.modules.basic.service.MtCcProductService;
import io.renren.modules.basic.service.MtCcTaskRunInfoDetService;
import io.renren.modules.work.Utils.ManagePublicMethod;
import io.renren.modules.work.dao.WorkDao;
import io.renren.modules.work.entity.WorkEntity;
import io.renren.modules.work.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: weihongyang
 * @Date: 2019/10/14 10:22
 * @Version 1.0
 */
@Service("workService")
public class WorkServiceImpl extends ServiceImpl<WorkDao,WorkEntity> implements WorkService {

    @Autowired
    private MtCcWarehousPositionRelatDao mtCcWarehousPositionRelatDao;

    @Autowired
    private MtCcTaskRunInfoDao mtCcTaskRunInfoDao;

    @Autowired
    private MtCcTaskRunInfoLogDao mtCcTaskRunInfoLogDao;

    @Autowired
    private MtCcTaskRunInfoDetDao mtCcTaskRunInfoDetDao;

    @Autowired
    private MtCcTaskRunInfoDetLogDao mtCcTaskRunInfoDetLogDao;

    @Autowired
    private MtCcWarehousRankDao mtCcWarehousRankDao;
    @Autowired
    private MtCcTaskRunInfoDetService mtCcTaskRunInfoDetService;
    @Autowired
    private MtCcProductService mtCcProductService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkOutForMainTask(Map<String, Object> params) {

        String taskCode = ManagePublicMethod.creatOrderCode("O", params.get("doorWay").toString());

        /**
         * 1.把手动下的主任务存到数据库中
         */
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = new MtCcTaskRunInfoEntity();
        mtCcTaskRunInfoEntity.setCreateTime(new Date());
        mtCcTaskRunInfoEntity.setUserId(Integer.parseInt(params.get("userId").toString()));
        mtCcTaskRunInfoEntity.setUserName(params.get("userName").toString());
        mtCcTaskRunInfoEntity.setTaskType(0);
        mtCcTaskRunInfoEntity.setStatus(0);
        mtCcTaskRunInfoEntity.setTaskCode(taskCode);
        mtCcTaskRunInfoEntity.setProductId(Integer.parseInt(params.get("productId").toString()));
        mtCcTaskRunInfoEntity.setModel(params.get("productName").toString());
        mtCcTaskRunInfoEntity.setNum(params.get("num").toString());
        mtCcTaskRunInfoEntity.setUnit(params.get("unit").toString());
        int orderNum = mtCcTaskRunInfoDao.selectCount(new QueryWrapper<>());
        mtCcTaskRunInfoEntity.setOrderNum(orderNum+1);
        mtCcTaskRunInfoDao.insert(mtCcTaskRunInfoEntity);

        /**
         * 2.把手动下的主任务的日志存到数据库
         */
        MtCcTaskRunInfoLogEntity mtCcTaskRunInfoLogEntity = new MtCcTaskRunInfoLogEntity();
        mtCcTaskRunInfoLogEntity.setCreateTime(new Date());
        mtCcTaskRunInfoLogEntity.setUserId(Integer.parseInt(params.get("userId").toString()));
        mtCcTaskRunInfoLogEntity.setUserName(params.get("userName").toString());
        mtCcTaskRunInfoLogEntity.setTaskType(0);
        mtCcTaskRunInfoLogEntity.setStatus(0);
        mtCcTaskRunInfoLogEntity.setTaskCode(taskCode);
        mtCcTaskRunInfoLogEntity.setProductId(Integer.parseInt(params.get("productId").toString()));
        mtCcTaskRunInfoLogEntity.setModel(params.get("productName").toString());
        mtCcTaskRunInfoLogEntity.setNum(params.get("num").toString());
        mtCcTaskRunInfoLogEntity.setUnit(params.get("unit").toString());
        mtCcTaskRunInfoLogDao.insert(mtCcTaskRunInfoLogEntity);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R checkOutMainTaskForDet(Map<String, Object> params) {

        /**
         * 1.按照货物类型数量查找仓库
         */
        Integer productId = Integer.parseInt(String.valueOf(params.get("productId")));
        List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatDao.selectList(new QueryWrapper<MtCcWarehousPositionRelatEntity>().eq("product_id",productId));
        int num = 0;
        int checkOutNum = Integer.parseInt(params.get("checkOutNum").toString());
        List<MtCcWarehousPositionRelatEntity> relatEntityList = new ArrayList<>();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list) {
            num = num + mtCcWarehousPositionRelatEntity.getQuantity();
            relatEntityList.add(mtCcWarehousPositionRelatEntity);
            if (num >= checkOutNum) {
                break;
            }
        }

        /**
         * 2.判断仓库是否满足出库的数量
         */
        if (num < checkOutNum) {
            return R.error("仓库中"+params.get("productName")+"数量为"+num+",你要出库的数量为"+checkOutNum+",仓库数量不够出库！").put("num",+num);
        }

        /**
         * 3.修改总任务表中状态字段
         * 未执行改变成执行中
         */
/*        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = new MtCcTaskRunInfoEntity();
        mtCcTaskRunInfoEntity.setStatus(4);
        mtCcTaskRunInfoDao.update(mtCcTaskRunInfoEntity,new QueryWrapper<MtCcTaskRunInfoEntity>().eq("id",
                params.get("id")));*/

        /**
         * 4.修改总任务日志的状态字段
         * 未执行改变成执行中
         */
        /*MtCcTaskRunInfoLogEntity mtCcTaskRunInfoLogEntity = new MtCcTaskRunInfoLogEntity();
        mtCcTaskRunInfoLogEntity.setStatus(1);
        mtCcTaskRunInfoDao.update(mtCcTaskRunInfoEntity,new QueryWrapper<MtCcTaskRunInfoEntity>().eq("task_code",
                params.get("taskCode")));
*/
        /**
         * 4.把总任务分配详细任务,并且记录日志
         */
        List<MtCcTaskRunInfoDetEntity> mtCcTaskRunInfoDetEntityList = new ArrayList<>();
        List<MtCcTaskRunInfoDetLogEntity> mtCcTaskRunInfoDetLogEntityList = new ArrayList<>();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:relatEntityList) {
            MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity = new MtCcTaskRunInfoDetEntity();
            String taskDetCode = ManagePublicMethod.creatOrderCode("O",params.get("doorWay").toString());
            mtCcTaskRunInfoDetEntity.setCreateTime(new Date());
            mtCcTaskRunInfoDetEntity.setWarehousRankId(mtCcWarehousPositionRelatEntity.getWarehousPositionId());
            mtCcTaskRunInfoDetEntity.setWarehousRankName(mtCcWarehousPositionRelatEntity.getWarehousPositionName());
            mtCcTaskRunInfoDetEntity.setTaskRunId(Integer.parseInt(params.get("id").toString()));
            mtCcTaskRunInfoDetEntity.setTaskType(0);
            mtCcTaskRunInfoDetEntity.setStatus(0);
            mtCcTaskRunInfoDetEntity.setTaskDetCode(taskDetCode);
            mtCcTaskRunInfoDetEntity.setModel(mtCcProductService.getById(mtCcWarehousPositionRelatEntity.getProductId()).getProductCode());
            mtCcTaskRunInfoDetEntity.setNum(mtCcWarehousPositionRelatEntity.getQuantity().toString());
            mtCcTaskRunInfoDetEntityList.add(mtCcTaskRunInfoDetEntity);

            /*MtCcTaskRunInfoDetLogEntity mtCcTaskRunInfoDetLogEntity = new MtCcTaskRunInfoDetLogEntity();
            mtCcTaskRunInfoDetLogEntity.setCreateTime(new Date());
            mtCcTaskRunInfoDetLogEntity.setWarehousRankId(mtCcWarehousPositionRelatEntity.getWarehousPositionId());
            mtCcTaskRunInfoDetLogEntity.setWarehousRankName(mtCcWarehousPositionRelatEntity.getWarehousPositionName());
            mtCcTaskRunInfoDetLogEntity.setTaskRunId(Integer.parseInt(params.get("id").toString()));
            mtCcTaskRunInfoDetLogEntity.setTaskType(0);
            mtCcTaskRunInfoDetLogEntity.setStatus(0);
            mtCcTaskRunInfoDetLogEntity.setTaskDetCode(taskDetCode);
            mtCcTaskRunInfoDetLogEntity.setModel(mtCcWarehousPositionRelatEntity.getProductName());
            mtCcTaskRunInfoDetLogEntity.setNum(mtCcWarehousPositionRelatEntity.getQuantity().toString());
            mtCcTaskRunInfoDetLogEntityList.add(mtCcTaskRunInfoDetLogEntity);*/


        }

        //mtCcTaskRunInfoDetDao.saveList(mtCcTaskRunInfoDetEntityList);
        mtCcTaskRunInfoDetService.saveBatch(mtCcTaskRunInfoDetEntityList);
        //mtCcTaskRunInfoDetLogDao.saveList(mtCcTaskRunInfoDetLogEntityList);
        return R.ok();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeIssueTask(Map<String, Object> params) {
        /**
         * 修改点击的任务中的状态
         * 把未执行改为正在执行
         */
        MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity = new MtCcTaskRunInfoDetEntity();
        mtCcTaskRunInfoDetEntity.setStatus(1);
        mtCcTaskRunInfoDetDao.update(mtCcTaskRunInfoDetEntity,new QueryWrapper<MtCcTaskRunInfoDetEntity>().eq("id",params.get("id")));
        /**
         * 修改点击的任务日志中的状态
         * 把未执行改为正在执行
         */
        MtCcTaskRunInfoDetLogEntity mtCcTaskRunInfoDetLogEntity = new MtCcTaskRunInfoDetLogEntity();
        mtCcTaskRunInfoDetLogEntity.setStatus(1);
        mtCcTaskRunInfoDetLogDao.update(mtCcTaskRunInfoDetLogEntity,
                new QueryWrapper<MtCcTaskRunInfoDetLogEntity>().eq("task_det_code",params.get("task_det_code")));

        /**
         * 找到该出库任务的仓库位置数据
         */
        MtCcWarehousRankEntity mtCcWarehousRankEntity = mtCcWarehousRankDao.selectById(params.get("warehousRankId").toString());
        

    }

    @Override
    public void test() {

    }

}
